package com.woniu.ui;

import com.woniu.dao.StudentDao;
import com.woniu.dao.impl.StudentDaoImpl;
import com.woniu.entity.Student;
import com.woniu.exception.NameExsitsException;
import com.woniu.service.StudentService;
import com.woniu.service.impl.StudentServiceImpl;

import java.util.List;
import java.util.Scanner;

public class Application {

    public static void main(String[] args) {
        //注册
//        register();
        //登录
//        login();
        //显示所有学生信息
//        getStudentAll();
        //删除学生
//        Deletre();
        //修改学生
//        update();
    }

    private static void update() {
        Scanner sc = new Scanner(System.in);
        StudentDao studentDao = new StudentDaoImpl();
        String name;
        while (true) {
            System.out.println("请输入要修改人的姓名");
            name = sc.next();
            if(studentDao.selectStudentName(name) == null){
                System.out.println("名字不存在");
            }else {
                break;
            }
        }
        List<Student> students = studentDao.selectStudentAll();
        int sub=0;
        for (Student student : students) {
            sub++;
            if (student.getName().equals(name)) {
                break;
            }
        }
        StudentService studentService = new StudentServiceImpl();
        studentService.UpdateStudent(sub-1);
    }

    private static void Deletre() {
        Scanner sc = new Scanner(System.in);
        StudentDao studentDao = new StudentDaoImpl();
        String name;
        while (true) {
            System.out.println("请输入要删除人的姓名");
            name = sc.next();
            if(studentDao.selectStudentName(name) == null){
                System.out.println("名字不存在");
            }else {
                break;
            }
        }
        List<Student> students = studentDao.selectStudentAll();
        int sub=0;
        for (Student student : students) {
            sub++;
            if (student.getName().equals(name)) {
                break;
            }
        }
        StudentService studentService = new StudentServiceImpl();
        studentService.DeleteStudent(sub-1);
    }

    private static void getStudentAll() {
        StudentService studentService = new StudentServiceImpl();
        studentService.GetStudentAll();
    }

    private static void login() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名");
        String name = sc.nextLine();
        System.out.println("请输入手机号");
        String phone = sc.nextLine();
        StudentService register= new StudentServiceImpl();
        try {
            register.StudentLogin(name,phone);
            System.out.println("登录成功");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private static void register() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入id");
        String id = sc.nextLine();
        System.out.println("请输入姓名");
        String name = sc.nextLine();
        System.out.println("请输入年龄");
        String age = sc.nextLine();
        System.out.println("请输入性别");
        String gender = sc.nextLine();
        System.out.println("请输入手机号");
        String phone = sc.nextLine();
        Student student = new Student(id, name, age, gender, phone);
        StudentService register= new StudentServiceImpl();
        try {
            register.StudentRegister(student);
            System.out.println("注册成功");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
